/* vim:ft=objc:
 * $Id: Folder.m 148 2005-05-29 20:15:44Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */


#import "Folder.h"


@implementation Folder

- (id) initWithName: (NSString*) n andFullPath: (NSString*) fp
{
	if ( (self = [super init]) != Nil ) {
		name = [n copy];
		fullPath = [fp copy];
		messages = [[NSMutableArray alloc] init];
		subFolders = [[NSMutableArray alloc] init];
		total = unread = new = 0;
		masked = NO;
		disabled = NO;
		canContainMail = YES;
	}
	return self;
}


- (void) dealloc
{
	[name release];
	[fullPath release];
	[messages release];
	[subFolders release];
	[super dealloc];
}



- (NSMutableArray*) subFolders
{
	return subFolders;
}


- (void) addSubFolder: (Folder*)f
{
	if ( [subFolders containsObject: [f name]] ) return;
	[subFolders addObject: f];
}


- (void) addSubFolderNamed: (NSString*)n
{
	if ( [subFolders containsObject: n] ) return;
	NSString *fp = [NSString stringWithFormat: @"%@/%@",
		 fullPath, n];
	[subFolders addObject: [[Folder alloc] initWithName: n andFullPath: fp]];
}


- (void) delSubFolderNamed: (NSString*)n
{
	if ( ![subFolders containsObject: n] ) return;
	[subFolders removeObject: n];
}


- (BOOL) wantsShown;
{
	BOOL subWantShown = NO;
	Folder *folder = Nil;
	NSEnumerator *folEnum = [subFolders objectEnumerator];

	while ( !subWantShown && (folder = [folEnum nextObject]) ) {
		subWantShown = (subWantShown || [folder wantsShown]);
	}

	return (subWantShown || (unread > 0));
}


- (NSString*) name
{
	return name;
}


- (NSString*) fullPath
{
	return fullPath;
}


- (void) addMessage: (Message*) message
{
	if ( [messages containsObject: message] ) return;
	[messages addObject: message];
	[messages sortUsingSelector: @selector(compare:)];

	if ( [message unread] ) unread++;
	if ( [message new] ) new++;
	total++;
}


- (NSArray*) messageList
{
	return messages;
}


- (void) resetMessageList
{
	[messages removeAllObjects];
	total = unread = new = 0;
}


- (NSMenu*) messageMenu
{
	BOOL addedItem = NO;
	Message *msg;
	NSEnumerator *msgEnum = [messages objectEnumerator];
	NSMenu *menu = [[NSMenu alloc] initWithTitle: name];

	while ( (msg = [msgEnum nextObject]) ) {
		NSMenuItem *mi = [msg menuItem];
		if ( mi == Nil ) continue;

		if ( [mi menu] != Nil ) {
			[[mi menu] removeItem: mi];
		}

		[menu addItem: mi];
		addedItem = YES;
	}

	if ( addedItem ) {
		return [menu autorelease];
	} else {
		[menu release];
		return Nil;
	}
}


- (unsigned) newMail
{
	return new;
}


- (BOOL) hasNew: (id) obj
{
	return (new > 0);
}


- (void) setNew: (unsigned) n
{
	new = n;
}


- (unsigned) unreadMail
{
	return unread;
}


- (BOOL) hasUnread: (id) obj
{
	return (unread > 0);
}


- (void) setUnread: (unsigned) u
{
	unread = u;
}


- (unsigned) totalMail
{
	return total;
}

- (void) setTotal: (unsigned) t
{
	total = t;
}


- (BOOL) masked
{
	return masked;
}


- (void) setMasked: (BOOL) f recursively: (BOOL) r
{
	unsigned int i;

	masked = f;

	if ( r ) {
		for ( i = 0 ; i < [subFolders count] ; ++i ) {
			[[subFolders objectAtIndex: i] setMasked: f
				recursively: YES];
		}
	}
}


- (BOOL) disabled
{
	return disabled;
}


- (void) setDisabled: (BOOL) f recursively: (BOOL) r
{
	unsigned int i;

	disabled = f;

	if ( r ) {
		for ( i = 0 ; i < [subFolders count] ; ++i ) {
			[[subFolders objectAtIndex: i] setDisabled: f
				recursively: YES];
		}
	}
}


- (void) touched: (id) obj
{
	NSMenuItem *mi = (NSMenuItem*)obj;

	masked = !masked;

	[mi setState: (masked) ? NSOffState : NSOnState];
	[[mi menu] itemChanged: mi];
}


- (BOOL) canContainMail
{
	return canContainMail;
}


- (void) setCanContainMail: (BOOL) c
{
	canContainMail = c;
}


- (unsigned) hash
{
	return [name hash];
}


- (BOOL) isEqual: (id) obj
{
	return ([self hash] == [obj hash]);
}


- (NSComparisonResult) compare: (Folder*) fol
{
	return [name compare: [fol name]];
}


@end
